package cn.iocoder.yudao.module.crm.controller.admin.clue;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.crm.controller.admin.clue.vo.robotdistributeconfig.CrmRobotClueDistributeConfigRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.clue.vo.robotdistributeconfig.CrmRobotClueDistributeConfigSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.clue.CrmRobotClueDistributeConfigDO;
import cn.iocoder.yudao.module.crm.service.clue.CrmRobotClueDistributeConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - CRM 线索分配配置")
@RestController
@RequestMapping("/crm/robot-clue-distribute-config")
@Validated
public class CrmRobotClueDistributeConfigController {

    @Resource
    private CrmRobotClueDistributeConfigService robotClueRDistributeConfigService;

    @GetMapping("/get")
    @Operation(summary = "获取线索分配规则设置")
    @OperateLog(enable = false)
    @PreAuthorize("@ss.hasPermission('crm:module:query')")
    @Parameter(name = "moduleId", required = true, description = "所属模块必填")
    public CommonResult<CrmRobotClueDistributeConfigRespVO> getConfig(@RequestParam("moduleId") Long moduleId) {
        CrmRobotClueDistributeConfigDO poolConfig = robotClueRDistributeConfigService.getClueDistributeConfigByModule(moduleId);
        return success(BeanUtils.toBean(poolConfig, CrmRobotClueDistributeConfigRespVO.class));
    }

    @PostMapping("/save")
    @Operation(summary = "更新线索分配规则设置")
    @PreAuthorize("@ss.hasPermission('crm:module:update')")
    public CommonResult<Boolean> saveConfig(@Valid @RequestBody CrmRobotClueDistributeConfigSaveReqVO updateReqVO) {
        robotClueRDistributeConfigService.saveCluePoolConfig(updateReqVO);
        return success(true);
    }

}
